package org.mistfx.decoration.demo;

import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Misty on 2015/6/14.
 */
public class AnimateApp extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Button remove = new Button("Remove");

        Rectangle rect1 = new Rectangle(50, 50, Color.ALICEBLUE);
        Rectangle rect2 = new Rectangle(50, 50, Color.ORANGERED);
        Rectangle rect3 = new Rectangle(50, 50, Color.AZURE);
        HBox pane = new HBox(rect1, remove, rect2) {
            private boolean childrenChanged;

            {
                getChildren().addListener((Observable observable) -> {
                    System.out.println("children changed");
                    childrenChanged = true;
                });
                getChildren().addListener((ListChangeListener<Node>) c -> {
                    System.out.println("change " + c);
                });
            }

            @Override
            protected void layoutChildren() {
                if (childrenChanged) {
                    List<Node> children = getManagedChildren();
                    List<Point2D> list1 = children.stream()
                            .map(node -> new Point2D(node.getLayoutX(), node.getLayoutY()))
                            .collect(Collectors.toList());
                    super.layoutChildren();
                    List<Point2D> list2 = children.stream()
                            .map(node -> new Point2D(node.getLayoutX(), node.getLayoutY()))
                            .collect(Collectors.toList());

                    for (int i = 0; i < list1.size(); i++) {
                        Node child = children.get(i);
                        Point2D b1 = list1.get(i);
                        Point2D b2 = list2.get(i);

                        Point2D t = b1.subtract(b2);
                        if (t.getX() != 0 || t.getY() != 0) {
                            child.setTranslateX(t.getX());
                            child.setTranslateY(t.getY());
                            TranslateTransition tt = new TranslateTransition(Duration.millis(300), child);
//                        tt.setFromX(t.getX());
//                        tt.setFromY(t.getY());
                            tt.setToX(0);
                            tt.setToY(0);
                            tt.play();
                        }
                    }

                    childrenChanged = false;
                } else {
                    super.layoutChildren();
                }


                System.out.println("layout children");
            }
        };
        pane.setSpacing(20);

        Scene scene = new Scene(pane, 600, 500);
        primaryStage.setScene(scene);
        primaryStage.show();

//        TranslateTransition transition = new TranslateTransition(Duration.millis(2000), rect);
//        transition.setToX(500);
//        transition.play();
        remove.setOnAction(event -> {
            FXCollections.replaceAll(pane.getChildren(), rect2, rect3);
        });
    }
}
